package vn.anhqn.englishsentensetranslator.base.fragment;

import vn.anhqn.englishsentensetranslator.activity.MainActivity;
import vn.anhqn.englishsentensetranslator.base.activity.BaseFragmentActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public abstract class BaseAppFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		setTitleHeader(getidTitle());
		View view = inflater.inflate(getResLayout(), null);
		setBerhaviorView(view);
		return view;
	}

	public ImageView getLeftHeaderIcon() {
		if (getActivity() instanceof MainActivity)
			return ((MainActivity) getActivity()).getLeftHeaderIcon();
		return null;
	}

	public abstract void setBerhaviorView(View mView);

	public abstract int getResLayout();

	public abstract int getidTitle();

	public void setLeftHeaderIcon(int resId) {
		if (getLeftHeaderIcon() != null)
			getLeftHeaderIcon().setImageResource(resId);
	}

	public void setTitleHeader(String title) {
		if (getActivity() instanceof MainActivity)
			((MainActivity) getActivity()).setHeaderTitle(title);
	}

	public void setTitleHeader(int idTitle) {
		if (idTitle != 0 && getActivity() instanceof MainActivity)
			((MainActivity) getActivity()).setHeaderTitle(getActivity().getResources().getString(idTitle));
	}

	public SlidingMenu getSlidingMenu() {
		if (getActivity() instanceof MainActivity)
			return ((MainActivity) getActivity()).getSlidingMenu();
		return null;
	}

	protected final BaseFragmentActivity getBaseActivity() {
		return (BaseFragmentActivity) getActivity();
	}

	// Methods switch fragment
	public int switchFragment(Class<? extends Fragment> fragment) {
		return getBaseActivity().switchFragment(fragment);
	}

	public int switchFragment(Class<? extends Fragment> fragment, Bundle pBundle) {
		return getBaseActivity().switchFragment(fragment, pBundle);
	}

	protected int switchFragment(Class<? extends Fragment> fragment, Bundle pBundle, boolean isReplace) {
		return getBaseActivity().switchFragment(fragment, pBundle, isReplace);
	}

	protected int switchFragment(Class<? extends Fragment> fragment, String tag, Bundle pBundle, boolean isReplace) {
		return getBaseActivity().switchFragment(fragment, tag, pBundle, isReplace);
	}
	 public void replaceNewInstance(Class<? extends Fragment> fragment ) {
		 getBaseActivity().replaceNewInstance(fragment);
	 }
	 public void replaceNewInstance(Class<? extends Fragment> fragment, Bundle mBundle ) {
		 getBaseActivity().replaceNewInstance(fragment,mBundle);
	 }
	 
	public void finishThis() {
		getBaseActivity().finishCurrentFragment();
	}
	
	public void setEnableSlidingMenu(boolean isEnable) {
		((MainActivity)getActivity()).enableSlidingMenu(isEnable);
	}
	
	@Override
	public void onStop() {
		Log.v("fragmentlife", "onStop");
		super.onStop();
	}
	@Override
	public void onDestroyView() {
		Log.v("fragmentViewDestroy", "viewDestroyed");
		super.onDestroyView();
	}
}
